//
//  PKPlayMainViewController.m
//  PianKe
//
//  Created by ybq on 15/12/25.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKPlayMainViewController.h"
#import "PKPlayListTableViewController.h"
#import "PKPlayInfoViewController.h"
#import "PKPlayOtherViewController.h"
#import "PKRadioTingModel.h"
#import "PKMoreTingModel.h"
#import "PKRadioMainViewController.h"
#import "PKPlayListTableViewCell.h"
@interface PKPlayMainViewController () <ChangeMusicDelegate, UIScrollViewDelegate, UMSocialUIDelegate>

@property (nonatomic, strong) PKPlayListTableViewController *listTableVC;
@property (nonatomic, strong) PKPlayInfoViewController *infoVC;
@property (nonatomic, strong) PKPlayOtherViewController *otherVC;
@property (nonatomic, strong) UIScrollView *mainScrollView;
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) PKMusicPlayer *player;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) PKRadioTingModel *model;
@property (nonatomic, strong) NSMutableArray *moreTingArray;

@end

@implementation PKPlayMainViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    _moreTingArray = [NSMutableArray array];
    _model = [[PKRadioTingModel alloc] init];
    self.view.backgroundColor = [UIColor whiteColor];
    UIView *stateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 20)];
    stateView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:stateView];
    [self creatPlayer];
    [self requestData];
    // Do any additional setup after loading the view.
    [self creatUI];
    [self creatVC];
}

- (void)creatPlayer {

    _player = [PKMusicPlayer sharePlayer];
    NSMutableArray *array = [NSMutableArray array];
    for (PKRadioDetailModel *model in _listArray) {
        [array addObject:model.musicUrl];
    }
    _player.musicArray = array;
    if (_player.index != _index) {
        [_player changeMusicWithIndex:_index];
    }
    
    NSLog(@"%ld",_player.index);
}

// 第四个页面数据请求
- (void)requestData {
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"07EBFC18-9BF8-47D1-9FEA-79BBF44D4C9E";
    parDic[@"version"] = @"3.0.6";
    parDic[@"tingid"] = _tingid;
    [_moreTingArray removeAllObjects];
    for (UIView *view in _otherVC.view.subviews) {
        if (view.tag > 100) {
            [view removeFromSuperview];
        }
    }
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/ting/info" parDic:parDic Method:POST finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *infoDic = dataDic[@"data"];
        [_model setValuesForKeysWithDictionary:infoDic];
        for (NSDictionary *dic in infoDic[@"moreting"]) {
            PKMoreTingModel *model = [[PKMoreTingModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_moreTingArray addObject:model];
        }
        [self creatInfoUI];
        [self voluation];
    } error:^(NSError *error) {
        
    }];
}

// 在数组加载完成后才对info的moreting进行创建,且更多电台按钮frame会改变
- (void)creatInfoUI {
    
    if (_moreTingArray.count != 0) {
        int num = (int)_moreTingArray.count;
        for (int i = 0; i < num; i++) {
            
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30 + 110 * (i % 3), 200 + 110 * (i / 3), 100, 100)];
        imageView.tag = i + 1000;
                
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 82, 100, 20)];
        label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"labelimage"]];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:11];
        label.tag = i + 100;
        [imageView addSubview:label];
        [_otherVC.view addSubview:imageView];
            
        }
    }
    
    UIButton *allRadioButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    allRadioButton.tag = 300;
    if (_moreTingArray.count > 3) {
        allRadioButton.frame = CGRectMake(kWidth - 125, 440, 80, 15);
    } else {
    allRadioButton.frame = CGRectMake(kWidth - 125, 320, 80, 15);
    }
    [allRadioButton setTitle:@"查看全部电台" forState:(UIControlStateNormal)];
    allRadioButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [allRadioButton setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [allRadioButton addTarget:self action:@selector(allRadioAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIImageView *nextImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth - 45, allRadioButton.frame.origin.y, 16, 16)];
    nextImageView.tag = 400;
    nextImageView.image = [UIImage imageNamed:@"next"];
    [_otherVC.view addSubview:allRadioButton];
    [_otherVC.view addSubview:nextImageView];
    
}

// 点击事件
- (void)allRadioAction:(UIButton *)sender {
    PKRadioMainViewController *radioMainVC = [[PKRadioMainViewController alloc] init];
    [self.navigationController pushViewController:radioMainVC animated:YES];
}

// 第四个页面赋值
- (void)voluation {
    [_otherVC.anchorHeaderImageView sd_setImageWithURL:[NSURL URLWithString:_model.usericon]];
    _otherVC.anchorLabel.text = _model.useruname;
    [_otherVC.originalImageView sd_setImageWithURL:[NSURL URLWithString:_model.authoricon]];
    _otherVC.originalLabel.text = _model.authoruname;
    _otherVC.fromLabel.text = _model.radioname;
    
    // moreting
    for (int i = 0; i < _moreTingArray.count; i++) {
        UIImageView *imageView = [_otherVC.view viewWithTag:i + 1000];
        [imageView sd_setImageWithURL:[NSURL URLWithString:((PKMoreTingModel *)_moreTingArray[i]).coverimg]];
        UILabel *label = [imageView viewWithTag:i + 100];
        label.text = [NSString stringWithFormat:@" %@",((PKMoreTingModel *)_moreTingArray[i]).title];
    }
}


- (void)creatUI {
    
    PKNavigationView *navView = [[PKNavigationView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 45)];
    UIButton *modeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    modeButton.frame = CGRectMake(kWidth / 2 - 10, 10, 18, 18);
    [modeButton setImage:[UIImage imageNamed:@"xunhuan"] forState:(UIControlStateNormal)];
    [navView addSubview:modeButton];
    
    UIButton *collectButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    collectButton.frame = CGRectMake(modeButton.frame.origin.x + 50, 10, 18, 18);
    [collectButton setImage:[UIImage imageNamed:@"shoucang"] forState:(UIControlStateNormal)];
    [navView addSubview:collectButton];
    
    UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    shareButton.frame = CGRectMake(collectButton.frame.origin.x + 50, 10, 18, 18);
    [shareButton setImage:[UIImage imageNamed:@"share"] forState:(UIControlStateNormal)];
    [shareButton addTarget:self action:@selector(shareAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [navView addSubview:shareButton];
    
    UIButton *timerButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    timerButton.frame = CGRectMake(shareButton.frame.origin.x + 50, 10, 18, 18);
    [timerButton setImage:[UIImage imageNamed:@"miaobiao"] forState:(UIControlStateNormal)];
    [navView addSubview:timerButton];
    
    [navView.leftButton setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
    [navView.leftButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:navView];
    
    // 关闭scrollView自动布局
    self.automaticallyAdjustsScrollViewInsets = NO;
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 65 - 90)];
    _mainScrollView.contentSize = CGSizeMake(kWidth * 4, _mainScrollView.frame.size.height);
    _mainScrollView.contentOffset = CGPointMake(kWidth, 0);
    _mainScrollView.delegate = self;
    _mainScrollView.pagingEnabled = YES;
    _mainScrollView.showsVerticalScrollIndicator = NO;
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_mainScrollView];
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(45, kHeight - 65, kWidth - 90, 1)];
    line.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:line];
    
    UIButton *lastButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    lastButton.frame = CGRectMake(60, kHeight - 50, 25, 25);
    [lastButton addTarget:self action:@selector(lastButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [lastButton setImage:[UIImage imageNamed:@"shangyiqu"] forState:(UIControlStateNormal)];
    [self.view addSubview:lastButton];
    
    UIButton *playButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    playButton.frame = CGRectMake(kWidth / 2 - 17, kHeight - 55, 35, 35);
    [playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [playButton setImage:[UIImage imageNamed:@"pause"] forState:(UIControlStateNormal)];
    [self.view addSubview:playButton];
    
    UIButton *nextButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    nextButton.frame = CGRectMake(kWidth - 84, kHeight - 50, 25, 25);
    [nextButton addTarget:self action:@selector(nextButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [nextButton setImage:[UIImage imageNamed:@"xiayiqu"] forState:(UIControlStateNormal)];
    [self.view addSubview:nextButton];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((kWidth - 70) / 2, kHeight - 80, 70, 10)];
    _pageControl.numberOfPages = 4;
    _pageControl.currentPage = 1;
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    [self.view addSubview:_pageControl];
    
}

- (void)shareAction:(UIButton *)sender {
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5689e43fe0f55a6ada0014be"
                                      shareText:@"哈哈"
                                     shareImage:nil
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToDouban,nil]
                                       delegate:self];
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat x = scrollView.contentOffset.x;
    NSInteger num = x / kWidth;
    if (num == 4) {
        num = 0;
    }
    _pageControl.currentPage = num;
}

- (void)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)lastButtonAction:(UIButton *)sender {
    [_player lastMusic];
    [self changeAllViewsWithIndex:_player.index];
}

- (void)playButtonAction:(UIButton *)sender {
    if (_player.isPlaying) {
        [_player pause];
        [sender setImage:[UIImage imageNamed:@"play"] forState:(UIControlStateNormal)];
    } else {
        [_player play];
        [sender setImage:[UIImage imageNamed:@"pause"] forState:(UIControlStateNormal)];
    }
}

- (void)nextButtonAction:(UIButton *)sender {
    [_player nextMusic];
    [self changeAllViewsWithIndex:_player.index];
}

- (void)changeAllViewsWithIndex:(NSInteger)index {
    if (_player.index != index) {
        [_player changeMusicWithIndex:index];
    }
    [_listTableVC.tableView reloadData];
    PKRadioDetailModel *model = _listArray[index];
    _tingid = model.tingid;
    [self requestData];
    [_infoVC setViewWithListModel:model];
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:model.webview_url]]];
}


- (void)creatVC {
    _listTableVC = [[PKPlayListTableViewController alloc] init];
    _listTableVC.listArray = _listArray;
    _listTableVC.delegate = self;
    _listTableVC.tableView.frame = CGRectMake(0, 0, kWidth, _mainScrollView.frame.size.height);
    [_mainScrollView addSubview:_listTableVC.tableView];
    
    _infoVC = [[PKPlayInfoViewController alloc] init];
    _infoVC.view.frame = CGRectMake(kWidth, 0, kWidth, _mainScrollView.frame.size.height);
    [_infoVC setViewWithListModel:_listArray[_index]];
    [_mainScrollView addSubview:_infoVC.view];
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(kWidth * 2, 0, kWidth, _mainScrollView.frame.size.height)];
    _webView.scalesPageToFit = YES;
    PKRadioDetailModel *model = _listArray[_index];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:model.webview_url]];
    [_webView loadRequest:request];
    [_mainScrollView addSubview:_webView];
    
    _otherVC = [[PKPlayOtherViewController alloc] init];
    _otherVC.view.frame = CGRectMake(kWidth * 3, 0, kWidth, _mainScrollView.frame.size.height);
    [_mainScrollView addSubview:_otherVC.view];
}

- (void)changeWithIndex:(NSInteger)index {
    [self changeAllViewsWithIndex:index];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
